class SuppliersController < ApplicationController
  before_action :set_supplier, only: [:show, :edit, :update, :destroy]
  before_action :set_page_title
  
  respond_to :html

  def index
    @suppliers = Supplier.order(created_at: :desc).page(params[:page])
    respond_with(@suppliers)
  end

  def show
    respond_with(@supplier)
  end

  def new
    @supplier = Supplier.new
    respond_with(@supplier)
  end

  def edit
  end

  def create
    @supplier = Supplier.new(supplier_params)
    
    respond_to do |format|
      if @supplier.save
        format.html { redirect_to suppliers_path, notice: 'Suppliers was successfully created.' }
        format.json { render :show, status: :created, location: @supplier }
      else
        format.html { render :new }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @supplier.update(supplier_params)
    respond_with(@supplier)
  end

  def destroy
    @supplier.destroy
    respond_with(@supplier)
  end

  private
    def set_supplier
      @supplier = Supplier.find(params[:id])
    end
    
    def set_page_title
      @page_title = "供应商管理" 
    end

    def supplier_params
      params.require(:supplier).permit(:name, :address, :tel, :fax, :tax_no, :bank, :bank_account)
    end
end
